// index.js
// 获取应用实例
const app = getApp()

Page({

  data: {
    indicatorDots: true,
    autoplay: true,
    interval: 5000,
    duration: 1000,
    contro: 0,
    imgUrls: [
      'https://img.zcool.cn/community/019e495c4bfcf7a801203d228149d2.jpg@1280w_1l_2o_100sh.jpg',
      'https://tse2-mm.cn.bing.net/th/id/OIP-C.5xW1vuXJHf03kcRpPkSgqgHaDt?w=319&h=175&c=7&r=0&o=5&dpr=1.3&pid=1.7',
      'https://tse2-mm.cn.bing.net/th/id/OIP-C.BvAyHcQTJxnYCXTIy99EnwHaDt?w=294&h=175&c=7&r=0&o=5&dpr=1.3&pid=1.7',
      'https://tse2-mm.cn.bing.net/th/id/OIP-C.mWlKt7R8qEFFXVYR5_Y7HAHaDt?w=350&h=175&c=7&r=0&o=5&dpr=1.3&pid=1.7',
      'https://tse2-mm.cn.bing.net/th/id/OIP-C.hZPBxbPlGzEcShq0iVRp1wHaEo?w=277&h=180&c=7&r=0&o=5&dpr=1.3&pid=1.7'
    ],
    movieInfo: [],
    wantWatchMovieInfo: [],

    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    canIUseGetUserProfile: false,
    canIUseOpenData: wx.canIUse('open-data.type.userAvatarUrl') && wx.canIUse('open-data.type.userNickName') // 如需尝试获取用户信息可改为false
  },
  // 事件处理函数
  //购票
  goupiao(e) {
    console.log(e)
    if (wx.getStorageSync('userInfo')!='') {
      wx.navigateTo({
        url: '../selectCinemaAndDate/selectCinemaAndDate?movieName=' + e.currentTarget.dataset.movieinfo.movieName
      })
      wx.setStorageSync('selectMovieId', e.currentTarget.dataset.movieinfo.id)
      wx.setStorageSync('selectMovieName', e.currentTarget.dataset.movieinfo.movieName)
    }else{
      wx.showToast({
        title: '请先登录',
        icon: 'error'
      })
    }
  },
  bindViewTap() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onLoad() {
    var that = this
    wx.request({
      url: 'http://localhost:8081/api/movie/wxGetMovieListForHot',
      method: 'GET',
      success: function (res) {
        that.setData({
          movieInfo: res.data.result
        })
      }
    })
    wx.request({
      url: 'http://localhost:8081/api/movie/wxGetMovieListForFuture',
      method: 'GET',
      success: function (res) {
        console.log(res)
        that.setData({
          wantWatchMovieInfo: res.data.result
        })
      }
    })
  },
  onShow() {
    var that = this
    if (that.data.contro == 0) {
      that.setData({
        contro : 1
      })
      return
    }
    if (wx.getStorageSync('username') != '' && that.data.contro == 1) {
      wx.request({
        url: 'http://localhost:8081/api/userInfo/getUserInfoByUsername',
        data: {
          username: wx.getStorageSync('username')
        },
        header: {
          'token': wx.getStorageSync('token')
        },
        method: "GET",
        success: function (res) {
          wx.setStorageSync('userInfo', res.data.result)
          that.setData({
            userInfo: res.data.result,
          })
        }
      })
    }
    wx.request({
      url: 'http://localhost:8081/api/movie/wxGetMovieListForHot',
      method: 'GET',
      success: function (res) {
        that.setData({
          movieInfo: res.data.result
        })
      }
    })
    wx.request({
      url: 'http://localhost:8081/api/movie/wxGetMovieListForFuture',
      method: 'GET',
      success: function (res) {
        console.log(res)
        that.setData({
          wantWatchMovieInfo: res.data.result
        })
      }
    })

  },

  toMovieDetail(e) {
    wx.navigateTo({
      url: '../movieDetail/movieDetail?movieId=' + e.currentTarget.dataset.tar.id,
    })
  },

  // 标记事件
  biaoji(e) {
    console.log(e)
    var that = this
    if (wx.getStorageSync('userInfo') == '') {
      wx.showToast({
        title: '请先登录',
        icon: 'error'
      })
      return
    }
    var collect = {
      userId: wx.getStorageSync('userInfo').id,
      movieId: e.currentTarget.dataset.movieinfo.id
    }
    wx.request({
      url: 'http://localhost:8081/api/collect/collectOrNot',
      method: 'POST',
      data: collect,
      header: {
        'token': wx.getStorageSync('token')
      },
      success: function (res) {
        if (res.data.code == 10008) {
          console.log(res.data.result)
          if (res.data.result == 1) {
            wx.showToast({
              title: '该电影已标记',
            })
          } else {
            wx.request({
              url: 'http://localhost:8081/api/collect/collectMovie',
              method: 'POST',
              data: collect,
              header: {
                'token': wx.getStorageSync('token')
              },
              success: function (res) {
                if (res.data.code == 10008) {
                  wx.showToast({
                    title: '标记成功',
                  })
                } else {
                  wx.showToast({
                    title: '数据错误',
                    icon: 'error'
                  })
                }
              }
            })
          }
        } else {
          wx.showToast({
            title: '数据错误',
            icon: 'error'
          })
        }
      }
    })
  },

  getUserProfile(e) {
    // 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认，开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
    wx.getUserProfile({
      desc: '展示用户信息', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res) => {
        console.log(res)
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    })
  },
  getUserInfo(e) {
    // 不推荐使用getUserInfo获取用户信息，预计自2021年4月13日起，getUserInfo将不再弹出弹窗，并直接返回匿名的用户个人信息
    console.log(e)
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  }
})